package controllers

import java.net.{URLDecoder, URLEncoder}

import dao._
import javax.inject.Inject
import org.joda.time.DateTime
import play.api.libs.json.Json
import play.api.mvc._
import tool._
import models.Tables._
import play.api.routing.JavaScriptReverseRouter
import shared.Shared
import utils.{DesEncrypter, Utils}
import utils.Utils.{Info, Sender}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
 * Created by yz on 2018/8/20
 */
class AppController @Inject()(cc: ControllerComponents, formTool: FormTool,
                              tool: Tool, pdfInfoDao: PdfInfoDao,
                              implicit val accountDao: AccountDao,
                              implicit val userDao: UserDao,
                             ) extends AbstractController(cc) {

  def loginBefore = Action { implicit request =>
    println("in")
    Ok(views.html.login())
  }

  def login = Action.async { implicit request =>
    val data = formTool.userForm.bindFromRequest().get
    accountDao.selectById1.zip(userDao.selectByUserData(data)).map { case (account, optionUser) =>
      if (data.name == account.account && data.password == account.password) {
        Redirect(routes.AdminController.userManageBefore).addingToSession(Shared.adminStr -> data.name)
      } else if (optionUser.isDefined) {
        val user = optionUser.get
        Redirect(routes.UserController.toHome).addingToSession(Shared.userStr -> user.name, Utils.idStr -> user.id.toString)
      } else {
        Redirect(routes.AppController.loginBefore).flashing("info" -> "用户名或密码错误!", "class" -> Utils.errorClass)
      }
    }
  }

  def javascriptRoutes = Action { implicit request =>
    Ok(
      JavaScriptReverseRouter("jsRoutes")(

        controllers.routes.javascript.PredictController.predictResult,
        controllers.routes.javascript.PredictController.predict,
        controllers.routes.javascript.PredictController.getLinearPlotData,
        controllers.routes.javascript.PredictController.batchPredict,

        controllers.routes.javascript.MissionController.deleteMissionById,
        controllers.routes.javascript.MissionController.getAllMission,
        controllers.routes.javascript.MissionController.deleteMissionByIds,

        controllers.routes.javascript.AdminController.getAllUser,
        controllers.routes.javascript.AdminController.userNameCheck,
        controllers.routes.javascript.AdminController.addUser,
        controllers.routes.javascript.AdminController.deleteUserById,
        controllers.routes.javascript.AdminController.getUserById,
        controllers.routes.javascript.AdminController.updateUser,

        controllers.routes.javascript.UserController.getPdfInfo,
        controllers.routes.javascript.UserController.updatePdfInfo,

        controllers.routes.javascript.ToolController.getFileData,
        controllers.routes.javascript.ToolController.openLiterPdf,
        controllers.routes.javascript.ToolController.getServerTime,

      )
    ).as("text/javascript")
  }


}
